package com.example.administrator.yltmusic;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class palyActivity extends Activity {

    MyServiceConn con;
    Intent intent;
    Music music;

    //用于设置音乐播放器的播放进度
    private static TextView tv_1;
    private static TextView tv_2;
    private static SeekBar seekBar;
    private static TextView txtv ;
    private Button shang,xia,bofang;
    public static int i;
    private String[] musics={"eight.mp3","elevent.mp3","five.mp3","four.mp3","nine.mp3","one.mp3","seven.mp3","six.mp3","ten.mp3","three.mp3","two.mp3"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paly);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        txtv = (TextView) findViewById(R.id.ttt);
        shang=(Button)findViewById(R.id.shang);
        xia=(Button)findViewById(R.id.xia);
        bofang=(Button)findViewById(R.id.bofang);


        bofang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv.setText(musics[0]);
                music.play();
            }
        });
        shang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (i>0){
                    i=i-1;
                }else{
                    i=10;
                }
                txtv.setText(musics[i]);
                music.last();
            }
        });
        xia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (i<10){
                    i=i+1;
                }else{
                    i=0;
                }
                txtv.setText(musics[i]);
                music.firit();
            }
        });

        //创建意图对象
        intent = new Intent(this, MyService.class);

        //启动服务
        startService(intent);

        //创建服务连接对象
        con = new MyServiceConn();

        //绑定服务
        bindService(intent, con, BIND_AUTO_CREATE);

        //获得布局文件上的滑动条
        seekBar = (SeekBar) findViewById(R.id.seekBar);

        //为滑动条添加事件监听
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            //当滑动条中的进度改变后,此方法被调用
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            //滑动条刚开始滑动,此方法被调用
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            //当滑动条停止滑动,此方法被调用
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

                //根据拖动的进度改变音乐播放进度
                int progress = seekBar.getProgress();

                //改变播放进度
                music.seekTo(progress);
            }
        });
    }


    //继续播放音乐按钮响应函数
    public void Play (View view) {

        //继续播放音乐
        music.Play();
    }


    //播放音乐按钮响应函数
    public void play(View view) {

        //播放音乐
        music.play();
    }


    //退出音乐播放按钮响应函数
    public void exit(View view) {

        //解绑服务
        unbindService(con);

        //停止服务
        stopService(intent);

        //结束这个activity
        finish();
    }
    //暂停播放音乐按钮响应函数
    public void pause(View view) {

        //暂停播放音乐
        music.pause();
    }
    //创建消息处理器对象



    public static Handler handler = new Handler(){

        //在主线程中处理从子线程发送过来的消息
        @Override
        public void handleMessage(Message message) {

            //获取从子线程发送过来的音乐播放的进度
            Bundle bundle = message.getData();

            //歌曲的总时长(毫秒)
            int stu = bundle.getInt("duration");

            //歌曲的当前进度(毫秒)
            int c = bundle.getInt("currentPosition");

            //刷新滑块的进度
            seekBar.setMax(stu);
            seekBar.setProgress(c);

            //歌曲的总时长
            int s = stu / 1000 % 60;
            int m = stu / 1000 / 60;


            String M = null;
            String S= null;

            //如果歌曲的时间中的分钟小于10
            if(m < 10) {

                //在分钟的前面加一个0
                M = "0" + m;
            } else {

                M = m+ "";
            }

            //如果歌曲的时间中的秒钟小于10
            if(s< 10)
            {
                //在秒钟前面加一个0
                S = "0" + s;
            } else {

                S= s+ "";
            }

            tv_2.setText( M + ":" + S);

            //歌曲当前播放时长
            m = c / 1000 / 60;
            s= c / 1000 % 60;

            //如果歌曲的时间中的分钟小于10
            if(m < 10) {

                //在分钟的前面加一个0
                M = "0" + m;
            } else {

                M = m + "";
            }

            //如果歌曲的时间中的秒钟小于10
            if(s < 10) {

                //在秒钟前面加一个0
               S = "0" + s;
            } else {

                S = s+ "";
            }

            tv_1.setText(M + ":" +S);
        }
    };
    //下一首
    public void next(View view){
        music.firit();
    }
    //上一首
    public void last(View view){
        music.last();
    }
    //实现服务器连接接口
    class MyServiceConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            //获得中间人对象
            music = (Music) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}